<template>
	<view>
		<!-- 提现 -->
		<view class="withdrawal-box">
			<view class="hd">
				<view class="u-flex" @click="pay.show = true">
					<view class="label"><text>充值到</text></view>
					<view class="u-flex-1">
						<view class="u-flex">
							<view class="u-flex-1">
								<!-- <u-icon v-if="way == 0" name="weixin-circle-fill" color="#2aae67" size="36"
									class="u-p-r-10"></u-icon>
								<u-icon v-if="way == 1" name="rmb-circle-fill" color="#ffaa00" size="36"
									class="u-p-r-10"></u-icon> -->
								<text class="u-font-32">{{operatorname}}</text>
							</view>
							<!-- <u-icon name="arrow-right" color="#909399"></u-icon> -->
						</view>
					</view>
				</view>
				<view class="tips"><text>本次操作将充值到'{{operatorname}}'账户中</text></view>
			</view>
			<view class="bd">
				<view class="u-font-28"><text>充值金额</text></view>
				<view class="u-flex">
					<u-icon name="rmb" color="#000" size="70"></u-icon>
					<input class="input-num" type="digit" v-model="amount">
				</view>
				<!-- <view class="error u-p-t-30" v-if="error">
					<text v-if="withdrawal > websetting.withdrawalsinglelimit">单笔提现最高{{websetting.withdrawalsinglelimit}}元</text>
					<text v-else>输入金额超过余额</text>
				</view>
				<view class="u-font-28 u-tips-color u-p-t-30" v-else>
					<text>当前余额{{balance}},提现不能少于0.1元,</text>
					<text class="u-type-primary" @click="withdrawal = balance">全部提现</text>
				</view> -->
				<view class="bottom-box u-p-t-30">
					<u-button type="primary" class="wbutton" :disabled="disabled" @click="recharge">充值</u-button>
					<view class="u-font-28 u-tips-color u-p-t-30">
						<view class="u-p-b-20"><text>温馨提示：</text></view>
						<!-- <view class="u-p-b-4"><text>1. 请先进行微信实名验证再提现。未实名验证的微信账号，提现会出现未到账情况；</text></view> -->
						<!-- <view class="u-p-b-4"><text>2. 由于微信相关规定，单笔提现最高{{websetting.withdrawalsinglelimit}}元，一天提现最高{{websetting.withdrawaldailylimit}}元；</text></view> -->
						<view class="u-p-b-4"><text>若出现未到账情况，请联系后台客服电话：<text class="u-type-primary" @click="call(websetting.telphone)">{{websetting.telphone}}</text></text></view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		name:"withdrawal",
		props:{
			userid:{
				type: Number,
				default: 0
			},
			enterpriseid:{
				type: Number,
				default: 0
			},
			all:{
				type: Number,
				default: 0
			},
		},
		data() {
			return {
				amount: null,
				disabled: true,
				error: false,
				// way: 0,
				balance: 0,
			};
		},
		computed: {
			...mapState({
				userdetail: state => state.userdetail,
				websetting: state => state.webSetting,
				operatorId: state => state.operatorid
			}),
			// 是否运营商管理员
			operatorname(){
				return this.userdetail.bindinfo.operator.name
			},
		},
		watch: {
			amount(val) {
				console.log(val)
				if (Number(val) <= 0) { //当到账方式是微信钱包时,超过后台设置的微信单次提现限额不能点击提现按钮
					this.disabled = true
					this.error = true
				} else {
					this.disabled = false
					this.error = false
				}
			}
		},
		onBackPress(e) {
			console.log('监听返回按钮事件', e);
			// uni.showToast({
			// 	title: e.from,
			// 	icon: 'none'
			// })
			// return false;
		},
		mounted() {
			console.log('websetting',this.websetting)
			this.$store.dispatch('getDetails').then((res)=>{
				// this.$refs.withdrawal.getStatistics()
				console.log('my页面成功获取用户详情：',res)
				// if(this.userid > 0){
				// 	this.balance = res.balance
				// }
				// if(this.enterpriseid > 0){
				// 	this.balance = res.bindinfo.enterpriselist.find((item)=>item.id == this.enterpriseid).balance
				// }
				// if(this.all > 0){
				// 	this.withdrawal = res.balancetotal
				// 	this.balance  = res.balancetotal
				// }
			}).finally(()=>{
				// this.loading = false
				uni.hideLoading()
			})
			// this.getStatistics()
		},
		methods: {
			close(){
				this.$emit('close',false)
			},
			// 充值金额
			recharge(){
				this.$u.post('/Operator/Recharge/Create',{
					amount: this.amount,
					way: 1
				}).then((res)=>{
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.package.timestamp,//时间戳从1970年1月1日至今的秒数，即当前的时间。
						nonceStr: res.package.noncestr,//随机字符串，长度为32个字符以下。//统一下单接口返回的 prepay_id 参数值，提交格式如：prepay_id=xx
						package:  res.package.prepayidpackage,
						signType: 'RSA',//签名算法，应与后台下单时的值一致
						paySign: res.package.signature,//签名
						success: function(res) {
							// console.log('success:', that.optionObj);
							//跳转页面
							uni.showToast({
								title: '支付成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta: -1
								})
							}, 1000)
						},
						fail: function(err) {
							console.log("支付失败的原因", err);
							uni.showToast({
								title: '取消支付',
								icon: 'none'
							})
							// var pages = getCurrentPages();
							// var currPage = pages[pages.length - 1]; //当前页面
							// var prevPage = pages[pages.length - 2]; //上一个页面
							// prevPage.$vm.showCar = false;
							// setTimeout(() => {
							// 	uni.navigateBack({
							// 		delta: -1
							// 	})
							// }, 800)
						}
					});
				})
			},
			// getWithdrawalApply() { //发起提现申请
			// 	console.log('发起提现申请',this.way)
			// 	if (this.way == 0) {
			// 		this.bindWeixin().then(() => {
			// 			this.withdrawalApply()
			// 		})
			// 	} else {
			// 		this.withdrawalApply()
			// 	}
			// },
			// withdrawalApply() { //提现申请方法
			// 	let form = {
			// 		item: [
			// 		    {
			// 				enterpriseId: this.enterpriseid || 0,
			// 				amount: Number(this.withdrawal)
			// 		    }
			// 		],
			// 		way: this.way
			// 	}
			// 	if(this.all > 0){
			// 		form.item = []
			// 		form.item.push({
			// 			enterpriseId: 0,
			// 			amount: this.userdetail.balance
			// 		})
			// 		this.userdetail.bindinfo.enterpriselist.forEach((item)=>{
			// 			form.item.push({
			// 				enterpriseId: item.id,
			// 				amount: item.balance
			// 			})
			// 		})
			// 		form.item = form.item.filter((item)=>item.amount > 0)
			// 	}
			// 	// 当到账方式选择的是银行账号时
			// 	if (this.way == 1) {
			// 		form.bankname = this.bankname,
			// 			form.bankaccount = this.bankaccount,
			// 			form.bankopenname = this.bankopenname
			
			// 		if (form.bankname == '') {
			// 			uni.showToast({
			// 				title: '请填写银行户名',
			// 				icon: 'none'
			// 			})
			// 			return
			// 		}
			// 		if (form.bankaccount == '') {
			// 			uni.showToast({
			// 				title: '请填写银行账号',
			// 				icon: 'none'
			// 			})
			// 			return
			// 		}
			// 		if (form.bankopenname == '') {
			// 			uni.showToast({
			// 				title: '请填写开户行',
			// 				icon: 'none'
			// 			})
			// 			return
			// 		}
			// 	}
			
			// 	this.$u.post('/WithdrawApply/Create', form).then((res) => {
			// 		uni.showToast({
			// 			title: res.msg
			// 		})
			// 		uni.redirectTo({
			// 			url: '/pages/my/wallet/success/success'
			// 		})
			// 	})
			// },
			bindWeixin() {
				let that = this
				return new Promise((resolve, reject) => {
					if (that.userdetail.openid == '') {
						wx.showModal({
							title: '您尚未绑定微信',
							content: '需要绑定已实名微信才能成功提现,是否帮您绑定微信并申请提现？',
							confirmText: "确定绑定",
							success: function(res) {
								if (res.confirm) {
									// #ifdef MP-WEIXIN
									wx.login({
										provider: 'weixin', //使用微信登录
										success: ret => {
											var code = ret.code
											that.$u.get('/User/Account/BindingWeiXin?code=' +code).then(data => {
												if (data.status == 0) {
													resolve(res.msg)
												}
											})
										},
										fail: err => {
											reject()
										}
									})
									// #endif
								} else if (res.cancel) {
									reject()
								}
							},
							complete:function(res){
								console.log('complete',res)
							}
						})
					} else {
						resolve()
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	// 提现
	.withdrawal-box {
		background-color: #f4f4f5;
		height: 100vh;
		display: flex;
		flex-direction: column;

		.w-header {
			display: flex;
			align-items: center;
			text-align: center;
			height: 44px;
		}
	
		.hd {
			padding: 60rpx;
	
			.label {
				color: $u-tips-color;
				width: 150rpx;
			}
	
			.tips {
				font-size: 24rpx;
				color: $u-tips-color;
				padding-left: 150rpx;
				margin-top: 10rpx;
			}
		}
	
		.bd {
			display: flex;
			flex-direction: column;
			flex: 1;
			background-color: #fff;
			border-top-left-radius: 24rpx;
			border-top-right-radius: 24rpx;
			padding: 60rpx;
	
			.u-flex {
				padding-top: 60rpx;
				border-bottom: 1px solid $u-border-color;
	
				.input-num {
					flex: 1;
					font-size: 100rpx;
					height: 140rpx;
				}
			}
	
			.bottom-box {
				flex: 1;
	
				.wbutton {
					// padding: 60rpx;
				}
			}
	
			.error {
				color: $u-type-error;
			}
		}
	}
</style>